//
//  CPStatus.swift
//  weibo
//
//  Created by zcp on 16/5/16.
//  Copyright © 2016年 zcp. All rights reserved.
//

import UIKit

class CPStatus: NSObject {

    /// 微博创建时间
    var created_at: String?
    
    /// 微博ID
    var id: Int64 = 0
    
    /// 微博信息内容
    var text: String?
    
    /// 微博来源
    var source: String?
    
    /// 转发数
    var reposts_count: Int = 0
    
    /// 评论数
    var comments_count: Int = 0
    
    /// 表态数
    var attitudes_count: Int = 0
    
    ///用户模型
    var user: CPUser?
    
    /// 微博配图: 数组里面是字典 // Int类型的数组[Int], 字典类型的数组 [String: AnyObject] -> [[String: AnyObject]]
    var pic_urls: [[String: AnyObject]]? {
    
        didSet{
            if let arr = pic_urls {
            
                pictureUrls = [NSURL]()
                
                for dict in arr{
                
                    if let urlString = dict["thumbnail_pic"] as? String
                    {
                        let url = NSURL(string: urlString)
                        pictureUrls?.append(url!)
                    }
                }
            
            }
        
        }
        
    }
    
    ///图片url数组
    var pictureUrls: [NSURL]?
    //字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    //拦截这个key为"user",因为这个valu是个字典,所以还要转模型
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user"
        {
        
            if let dict = value as? [String: AnyObject]{
                user = CPUser(dict: dict)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    //打印模型
    override var description: String {
        get{
        
            let keys = ["created_at", "id", "text", "source", "reposts_count", "comments_count", "attitudes_count", "pic_urls"]
            return "微博模型:\(dictionaryWithValuesForKeys(keys))"
        }
    }
}
